/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ins.entity.InsField;
import com.thinkgem.jeesite.modules.ins.service.InsFieldService;

/**
 * 检验报告书-字段Controller
 * @author fanqinglong
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insField")
public class InsFieldController extends BaseController {
	@Autowired
	private InsFieldService insFieldService;

	@ModelAttribute
	public InsField get(@RequestParam(required=false) String id) {
		InsField entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insFieldService.get(id);
		}
		if (entity == null){
			entity = new InsField();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insField:view")
	@RequestMapping(value = {"list", ""})
	public String list(InsField insField, HttpServletRequest request, HttpServletResponse response, Model model) {
		insField.setFrequently("1");
		Page<InsField> page = insFieldService.findPage(new Page<InsField>(request, response), insField); 
		model.addAttribute("page", page);
		model.addAttribute("insField", insField);
		return "modules/ins/insFrequentlyFiledList";
	}
	
	@RequiresPermissions("ins:insField:edit")
	@RequestMapping(value = "delete")
	public String delete(InsField insField,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		insFieldService.delete(insField);
		addMessage(redirectAttributes, "删除常用参数成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insField/?repage";
	}
	
	@RequiresPermissions("ins:insField:edit")
	@RequestMapping(value = {"form"})
	public String form(InsField insField,Model model){
		model.addAttribute("insField", insField);
		return "modules/ins/insFrequentlyFieldForm";
	}
	
	@RequiresPermissions("ins:insField:edit")
	@RequestMapping(value = {"save"})
	public String save(InsField insField,Model model,RedirectAttributes redirectAttributes){
		/*insField.setTable(new InsTable());
		if (!beanValidator(model, insField)){
			return form(insField, model);
		}*/
		insField.setFrequently("1");
		insFieldService.save(insField);
		addMessage(redirectAttributes, "保存常用字段成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insField/?repage";
	}
	
	@RequiresPermissions("ins:insField:edit")
	@RequestMapping(value = {"getByName"})
	@ResponseBody
	public String getByName(InsField insField){
		String result = "false";
		InsField resultField = insFieldService.getByName(insField);
		if (null == resultField || resultField.getId().equals(insField.getId())) {
			result = "true";
		}
		return result;
	}
	
	@RequiresPermissions("ins:insField:edit")
	@RequestMapping(value = {"getByCode"})
	@ResponseBody
	public String getByCode(InsField insField){
		String result = "false";
		InsField resultField = insFieldService.getByName(insField);
		if (null == resultField || resultField.getId().equals(insField.getId())) {
			result = "true";
		}
		return result;
	}
}
